package leetcode;

/**
 * @Project : ProgrammingExercises
 * @Package : Leetcode
 * @File : Q2566.java
 * @Author : WangRuoyu
 * @Date 2023/2/22 16:13
 */
public class Q2566 {
    public int minMaxDifference(int num) {
        String str = Integer.toString(num);

        int index;

        for (index = 0; index < str.length(); ++index) {
            if (str.charAt(index) != '9') {
                break;
            }
        }
        if (index == str.length()) {
            return num;
        }

        StringBuilder strMax = new StringBuilder();
        StringBuilder strMin = new StringBuilder();
        char x = str.charAt(0);
        char y = str.charAt(index);
        for (int i = 0; i < str.length(); ++i) {
            if (str.charAt(i) == x) {
                strMin.append('0');
            } else {
                strMin.append(str.charAt(i));
            }
            if (str.charAt(i) == y) {
                strMax.append('9');
            } else {
                strMax.append(str.charAt(i));
            }
        }
        int numMax = Integer.parseInt(String.valueOf(strMax));
        int numMin = Integer.parseInt(String.valueOf(strMin));

        return numMax - numMin;
    }

    public static void main(String[] args) {
        Q2566 sl = new Q2566();
        int num = 90;
        System.out.println(sl.minMaxDifference(num));
    }
}